<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateWaimaiPartRefundLogsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $cFunc = function (Blueprint $table) {

            $table->id();

            $table->unsignedInteger('shopId')->default(0)->comment('');
            $table->unsignedBigInteger('orderId')->default(0)->comment('外卖订单号');
            $table->unsignedBigInteger('callbackId')->default(0)->comment('waimai callback表id');
            $table->unsignedInteger('originalPrice')->default(0)->comment('减少的营业额');
            $table->unsignedInteger('payPrice')->default(0)->comment('用户收到的退款');
            $table->unsignedInteger('expectIncome')->default(0)->comment('减少的实收(已减少servicePrice)');
            $table->unsignedInteger('packingPrice')->default(0)->comment('减少的包装费');

            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('updatedAt')->default(0)->comment('更新时间');
            $table->tinyInteger('isDeleted')->default(0);

            $table->index(['createdAt'], 'idx_createdAt');
            $table->index(['callbackId'], 'idx_callbackId');
            $table->index(['orderId', 'shopId'], 'idx_orderId');

            $table->collation = 'utf8mb4_general_ci';
            $table->comment   = '外卖 - 部分退款日志';
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->create('waimai_part_refund_logs' . $this->name, $cFunc);
            Schema::connection("mysqlWaiMai_2")->create('waimai_part_refund_logs' . $this->name, $cFunc);

            dump($this->name . '已添加');
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->drop('waimai_part_refund_logs' . $this->name);
            Schema::connection("mysqlWaiMai_2")->drop('waimai_part_refund_logs' . $this->name);

            dump($this->name . '已添加');
        }
    }
}
